//
//  RechargeViewController.m
//  YunGou
//
//  Created by macbook on 16/5/20.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "RechargeViewController.h"
#import "RechargeMoneyCell.h"

@interface RechargeViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSArray *payTitleArray;
@property (nonatomic,strong) NSArray *payImageArray;
@property (nonatomic,assign) NSInteger selectedIndex;
@property (nonatomic,strong) UIView *bgView;
@property (nonatomic,assign) CGFloat moneyNum;
@end

@implementation RechargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _payTitleArray = @[@"微信支付",@"支付宝支付"];
    _payImageArray = @[@"shouye_cz_wxzf",@"shouye_cz_zfbzf"];
    _selectedIndex = 0;
    
    NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:1];
    [self.tableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionBottom];
    [self tableView:self.tableView didSelectRowAtIndexPath:ip];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealKeyboardShow) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealKeyboardHide) name:UIKeyboardWillHideNotification object:nil];
}

- (void)dealKeyboardShow{
    [self.view addSubview:self.bgView];
}

- (void)dealKeyboardHide{
    [self.bgView removeFromSuperview];
    self.bgView = nil;
    [self.view endEditing:NO];
}

- (IBAction)surePayClicked:(id)sender {
    FZLog(@"%f",_moneyNum);
}


#pragma -mark tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *baseCell = nil;
    if (indexPath.section == 0) {
        RechargeMoneyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"priceCell"];
        cell.block = ^(UIButton *btn){
            NSString *price = [btn getAccessoryObject];
            _moneyNum = [price floatValue];
        };
        baseCell = cell;
    }else{
        baseCell = [tableView dequeueReusableCellWithIdentifier:@"payTypeCell"];
        UILabel *payTitleLabel = [baseCell.contentView viewWithTag:2];
        payTitleLabel.text = _payTitleArray[indexPath.row];
        UIImageView *payImageView = [baseCell.contentView viewWithTag:1];
        payImageView.image = [UIImage imageNamed:_payImageArray[indexPath.row]];
    }
    return baseCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 1) {
        if (_selectedIndex != indexPath.row) {
            UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:1]];
            UIImageView *lastImageView = [lastCell.contentView viewWithTag:3];
            lastImageView.hidden = YES;
            
            _selectedIndex = indexPath.row;
            UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
            UIImageView *iconImg = [selectedCell.contentView viewWithTag:3];
            iconImg.hidden = NO;
        }else{
            UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
            UIImageView *iconImg = [selectedCell.contentView viewWithTag:3];
            iconImg.hidden = NO;
        }
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"选择充值金额（元）";
    }return @"选择支付方式";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 120;
    }return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0f;
}

#pragma -mark 懒加载
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:self.view.bounds];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenKeyBoard)];
        [_bgView addGestureRecognizer:tap];
    }
    return _bgView;
}

- (void)hiddenKeyBoard{
    [self.view endEditing:YES];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
